/// Author: Samle
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class EditBooksController extends GetxController {

  /// 获取Key用来获取Form表单组件
  GlobalKey<FormState> editBooksKey = GlobalKey<FormState>();
  TextEditingController name = TextEditingController();
  TextEditingController path = TextEditingController();
  TextEditingController cover = TextEditingController();
  TextEditingController num = TextEditingController();
  TextEditingController content = TextEditingController();
  TextEditingController source = TextEditingController();

  /// 数据表单
  Map dataForm = {
    "id": null,
    "name": null,
    "path": null,
    "cover": null,
    "num": null,
    "content": null,
    "source": null,
  };

  /// 数据表单
  Map cleanDataForm = {
    "id": null,
    "name": null,
    "path": null,
    "cover": null,
    "num": null,
    "content": null,
    "source": null,
  };

  /// 获取数据
  getData() async {
    try {
      if (Get.arguments.isNotEmpty && Get.arguments != null){
        dataForm = Get.arguments;
        name.text = dataForm['name'] != null ? dataForm['name'].toString() : '';
        path.text = dataForm['path'] != null ? dataForm['path'].toString() : '';
        cover.text = dataForm['cover'] != null ? dataForm['cover'].toString() : '';
        num.text = dataForm['num'] != null ? dataForm['num'].toString() : '';
        content.text = dataForm['content'] != null ? dataForm['content'].toString() : '';
        source.text = dataForm['source'] != null ? dataForm['source'].toString() : '';
      }
    } catch (e) {
      print("数据获取失败");
    } finally {
    }
  }

  /// 返回数据
  saveData() {
    dataForm['name'] = name.text;
    dataForm['path'] = path.text;
    dataForm['cover'] = cover.text;
    if (num.text != null && num.text != '' && num.text != 'null'){
      dataForm['num'] = int.parse(num.text);
    } else {
      dataForm['num'] = '';
    }
    dataForm['content'] = content.text;
    dataForm['source'] = source.text;
    Get.back(result: dataForm);
  }

  @override
  void onInit() {
    getData();
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
